package com.apes.scm.masterdata.finance.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.finance.model.BankAccount;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

/**
 * 功能：定义银行账号
 *
 * @Auther: Biebi
 * @Date: 2018-8-31 14:18:09
 */
public interface BankAccountRepository extends CustomRepository<BankAccount, String> {

    @Query("SELECT T FROM BankAccount T WHERE T.company.id = :company")
    Page<BankAccount> findByCompany(@Param("company") String company, Pageable pageable);

    @Query("SELECT T FROM BankAccount T WHERE T.company.id = :company and T.valid = true")
    List<BankAccount> findByCompanyAndValid(@Param("company") String company);

    @Query("SELECT T FROM BankAccount T WHERE T.company.id = :company AND (T.id LIKE %:key% OR T.name LIKE %:key%)")
    Page<BankAccount> findByCompanyAndKey(@Param("company")String company,@Param("key") String key, Pageable pageable);

    @Query(value = "SELECT T.* FROM mas_bank_account T WHERE T.ACCOUNT_ID = :bankAccountId AND ROWNUM = 1",nativeQuery = true)
    BankAccount findBankBranch(@Param("bankAccountId")String bankAccountId);

    @Query("SELECT A.id as id ,A.accountName as accountName FROM BankAccount A WHERE A.id = :id")
    Map<String, Object> findIdAndName(@Param("id") String merchantBankAccountId);

    @Query("SELECT T FROM BankAccount T WHERE T.company.id = :company and T.valid = true and T.prepareSign = true")
    List<BankAccount> findByCompanyAndPrepareSign(@Param("company") String company);
}
